﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace TelescopeScripting.Logic
{
    public static class Scripting
    {
        public static string ExecuteScript(string script, string rtguiPath, bool inPlaylist)
        {
            ProcessStartInfo executeInfo = new ProcessStartInfo();
            executeInfo.FileName = @"C:\rtgui\RTGUI.exe";
            executeInfo.Arguments = script;
            executeInfo.RedirectStandardOutput = true;
            executeInfo.UseShellExecute = false;
            executeInfo.CreateNoWindow = true;
            Process execute = Process.Start(executeInfo);
            if (inPlaylist)
            {
                execute.WaitForExit();
            }
            string output = "Output:\n";
            return output;
        }

        public static string ParseCommand(string command, string value)
        {
            return command + "=" + value;
        }

        public static string ParseCommands(ItemCollection commands)
        {
            string commandLine = "";

            foreach (string command in commands)
            {
                commandLine = commandLine + command + "; "; 
            }
            if (commandLine.Length > 2)
            {
                commandLine = commandLine.Substring(0, commandLine.Length - 2);
            }
            return commandLine;
        }

        public static string ParsePlayList(ItemCollection playlists)
        {
            string playList = "";

            foreach (string play in playlists)
            {
                playList = playList + play + ",";
            }
            if (playList.Length > 2)
            {
                playList = playList.Substring(0, playList.Length - 1); //remove the last comma
            }
            return playList;
        }

        public static string SetRTGUIPath()
        {
            string myDocsTelescoping = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\TelescopeScripting\rtguiPath.dat";
            
            if (!Directory.Exists(System.IO.Path.GetDirectoryName(myDocsTelescoping)))
            {
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(myDocsTelescoping));
            }

            string rtguiPath = "";
            if (File.Exists(myDocsTelescoping))
            {
                rtguiPath = File.ReadAllText(myDocsTelescoping);
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Title = "Find rtgui.exe and select it";
                dialog.InitialDirectory = @"C:\";

                if (dialog.ShowDialog() != null && !dialog.FileName.Equals(""))
                {
                    File.WriteAllText(myDocsTelescoping, dialog.FileName);
                   return dialog.FileName;
                }
            }
            return rtguiPath;
        }
    }
}
